package com.example.team6.controller;


import com.example.team6.entity.Course;
import com.example.team6.entity.Studentwork;
import com.example.team6.entity.resultt;
import com.example.team6.mapper.StudentworkMapper;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author team6
 * @since 2020-06-29
 */

@RestController
@CrossOrigin
//@RequestMapping("/team6/studentwork")
public class StudentworkController {

    @Autowired
    StudentworkMapper studentworkMapper;

    @PostMapping(value = "api/isdo")
    @ResponseBody
    public List<Studentwork> isdo (@RequestBody Studentwork requestUser){

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("stuid", requestUser.getStuid());
        map.put("workid", requestUser.getWorkid());
        List<Studentwork> studentworks = studentworkMapper.selectByMap(map);

        return studentworks;
    }


    @PostMapping(value = "api/stuworkup")
    @ResponseBody
    public resultt sturegister (@RequestBody Studentwork requestUser){

        System.out.println("**************" + requestUser.getStuname());

        resultt resultt = new resultt();
        Studentwork studentwork = new Studentwork();

        studentwork.setStuid(requestUser.getStuid());
        studentwork.setWorkid(requestUser.getWorkid());
        studentwork.setUrl(requestUser.getUrl());
        studentwork.setStuname(requestUser.getStuname());
        studentwork.setStatus("0");

        Integer result = studentworkMapper.insert(studentwork);

        if (null == result) {
            String massage = "账号密码错误";
            return new resultt(400);
        } else {
            System.out.println("test");
            Map<String, Object> map = new HashMap<String, Object>();
            resultt.setCode(200);
            map.put("id", studentwork.getId());
            resultt.setData(map);
        }
        return resultt;
    }

    @PostMapping(value = "api/getstuwork")
    @ResponseBody
    public List<Studentwork> getstuwork (@RequestBody Studentwork requestUser){

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", requestUser.getStatus());
        List<Studentwork> studentworks = studentworkMapper.selectByMap(map);

        return studentworks;
    }

    @PostMapping(value = "api/stuview")
    @ResponseBody
    public String[] stuview (@RequestBody Studentwork requestUser){
        String[] temp;
        int i = 0;

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", requestUser.getStatus());
        List<Studentwork> studentworks = studentworkMapper.selectByMap(map);
        temp = new String[studentworks.size()];
        for (Studentwork t:studentworks
             ) {
            System.out.println("----" + t.getScore());
            temp[i] = String.valueOf(t.getScore());
            i++;
        }

        return temp;
    }

//    correcting
@PostMapping(value = "api/correcting")
@ResponseBody
public resultt correcting (@RequestBody Studentwork requestUser){

    resultt resultt = new resultt();
    Studentwork studentwork = new Studentwork();
       studentwork.setScore(requestUser.getScore());
       studentwork.setStatus("1");
    studentwork.setId(requestUser.getId());
//    studentwork.setWorkid(requestUser.getWorkid());
//    studentwork.setUrl(requestUser.getUrl());
//    studentwork.setStatus("0");
   //     int a = requestUser.getId();
    Integer result = studentworkMapper.updateById(studentwork);

    if (null == result) {
        String massage = "账号密码错误";
        return new resultt(400);
    } else {
        System.out.println("test");
        Map<String, Object> map = new HashMap<String, Object>();
        resultt.setCode(200);
        map.put("id", studentwork.getId());
        resultt.setData(map);
    }
    return resultt;
}


}

